<!-- src/views/ytj/pos/index.vue -->
<template>
  <div class="app-container">
    <!-- 查询条件 -->
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="地区">
        <AreaSelect :model="areaQueryModel" @update:model="updateAreaQueryModel" />
      </el-form-item>
      <el-form-item label="服务点名称" prop="posName" label-width="100px">
        <el-input
          v-model="queryParams.posName"
          placeholder="请输入服务点名称"
          clearable
          style="width: 200px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="序列号" prop="serialCode">
        <el-input
          v-model="queryParams.serialCode"
          placeholder="请输入序列号"
          clearable
          style="width: 200px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="请选择状态"
          clearable
          style="width: 200px"
        >
          <el-option label="停用" :value="0" />
          <el-option label="正常" :value="1" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 操作按钮 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['ytj:pos:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['ytj:pos:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['ytj:pos:remove']"
        >删除</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <!-- 数据表格 -->
    <el-table v-loading="loading" :data="posList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="省" align="center" prop="province" />
      <el-table-column label="市" align="center" prop="city" />
      <el-table-column label="区/县" align="center" prop="area" />
      <el-table-column label="乡镇/街道" align="center" prop="country" />
      <el-table-column label="详细地址" align="center" prop="detailAddress" />
      <el-table-column label="服务点名称" align="center" prop="posName" />
      <el-table-column label="序列号" align="center" prop="serialCode" />
      <el-table-column label="状态" align="center" prop="status">
        <template #default="scope">
          <el-tag :type="scope.row.status === 1 ? 'success' : 'danger'">
            {{ scope.row.status === 1 ? '正常' : '停用' }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button
            type="primary"
            link
            icon="Edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['ytj:pos:edit']"
          >修改</el-button>
          <el-button
            type="primary"
            link
            icon="Delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['ytj:pos:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页组件 -->
    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改对话框 -->
    <el-dialog :title="title" v-model="open" width="600px" append-to-body>
      <el-form ref="posRef" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="地区">
          <AreaSelect :model="areaFormModel" @update:model="updateAreaFormModel" />
        </el-form-item>
        <el-form-item label="详细地址" prop="detailAddress">
          <el-input v-model="form.detailAddress" placeholder="请输入详细地址" />
        </el-form-item>
        <el-form-item label="服务点名称" prop="posName">
          <el-input v-model="form.posName" placeholder="请输入服务点名称" />
        </el-form-item>
        <el-form-item label="序列号" prop="serialCode">
          <el-input v-model="form.serialCode" placeholder="请输入序列号" />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio :label="1">正常</el-radio>
            <el-radio :label="0">停用</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted, computed } from 'vue'
import { ElMessageBox, ElMessage } from 'element-plus'
import { listYtjPos, getYtjPos, addYtjPos, updateYtjPos, delYtjPos } from '@/api/nongjt/pos.js'
import AreaSelect from "@/components/AreaSelect.vue"

// 数据状态
const posList = ref([])
const open = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const title = ref("")

// 查询参数
const queryParams = reactive({
  pageNum: 1,
  pageSize: 10,
  province: undefined,
  city: undefined,
  area: undefined,
  country: undefined,
  posName: undefined,
  serialCode: undefined,
  status: undefined
})

// 表单参数
const form = reactive({
  id: undefined,
  province: undefined,
  city: undefined,
  area: undefined,
  country: undefined,
  detailAddress: undefined,
  posName: undefined,
  serialCode: undefined,
  status: 1
})

// 表单校验规则
const rules = {
  province: [{ required: true, message: "省不能为空", trigger: "blur" }],
  city: [{ required: true, message: "市不能为空", trigger: "blur" }],
  area: [{ required: true, message: "区/县不能为空", trigger: "blur" }],
  country: [{ required: true, message: "乡镇/街道不能为空", trigger: "blur" }],
  posName: [{ required: true, message: "服务点名称不能为空", trigger: "blur" }],
  serialCode: [{ required: true, message: "序列号不能为空", trigger: "blur" }],
  status: [{ required: true, message: "状态不能为空", trigger: "change" }]
}

// 引用
const queryRef = ref()
const posRef = ref()

// 查询用地区模型
const areaQueryModel = computed(() => {
  return {
    province: queryParams.province || '',
    city: queryParams.city || '',
    area: queryParams.area || '',
    country: queryParams.country || ''
  }
})

// 表单用地区模型
const areaFormModel = computed(() => {
  return {
    province: form.province || '',
    city: form.city || '',
    area: form.area || '',
    country: form.country || ''
  }
})

// 更新查询地区模型
function updateAreaQueryModel(model) {
  if (model) {
    queryParams.province = model.province
    queryParams.city = model.city
    queryParams.area = model.area
    queryParams.country = model.country
  }
}

// 更新表单地区模型
function updateAreaFormModel(model) {
  if (model) {
    form.province = model.province
    form.city = model.city
    form.area = model.area
    form.country = model.country
  }
}

// 获取列表
function getList() {
  loading.value = true
  listYtjPos(queryParams).then(response => {
    posList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

// 搜索
function handleQuery() {
  queryParams.pageNum = 1
  getList()
}

// 重置查询
function resetQuery() {
  queryRef.value.resetFields()
  handleQuery()
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id)
  single.value = selection.length !== 1
  multiple.value = !selection.length
}

// 新增按钮操作
function handleAdd() {
  open.value = true
  title.value = "添加体打印机服务点"
  reset()
}

// 修改按钮操作
function handleUpdate(row) {
  const id = row.id || ids.value[0]
  getYtjPos(id).then(response => {
    form.id = response.data.id
    form.province = response.data.province
    form.city = response.data.city
    form.area = response.data.area
    form.country = response.data.country
    form.detailAddress = response.data.detailAddress
    form.posName = response.data.posName
    form.serialCode = response.data.serialCode
    form.status = response.data.status
    open.value = true
    title.value = "修改体打印机服务点"
  })
}

// 提交表单
function submitForm() {
  posRef.value.validate(valid => {
    if (valid) {
      if (form.id !== undefined) {
        updateYtjPos(form).then(response => {
          ElMessage({ showClose: true, message: "修改成功", type: "success" })
          open.value = false
          getList()
        })
      } else {
        addYtjPos(form).then(response => {
          ElMessage({ showClose: true, message: "新增成功", type: "success" })
          open.value = false
          getList()
        })
      }
    }
  })
}

// 删除按钮操作
function handleDelete(row) {
  const deleteIds = row.id || ids.value
  ElMessageBox.confirm('是否确认删除体打印机服务点编号为"' + deleteIds + '"的数据项？', "警告", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning"
  }).then(function() {
    return delYtjPos(deleteIds)
  }).then(() => {
    getList()
    ElMessage({ showClose: true, message: "删除成功", type: "success" })
  }).catch(() => {})
}

// 取消按钮
function cancel() {
  open.value = false
  reset()
}

// 表单重置
function reset() {
  form.id = undefined
  form.province = undefined
  form.city = undefined
  form.area = undefined
  form.country = undefined
  form.detailAddress = undefined
  form.posName = undefined
  form.serialCode = undefined
  form.status = 1
  posRef.value.resetFields()
}

// 初始化加载
onMounted(() => {
  getList()
})
</script>
